package com.evildog.workspace.common.util;

import com.evildog.workspace.common.exception.BadRequestException;
import com.evildog.workspace.common.intefaces.ICustomExceptionDesc;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Map;

/**
 * 改装spring的Assert，实现自己的Assert
 *
 * @see org.springframework.util.Assert
 */
public abstract class WsAssert {

    public static void state(boolean expression, ICustomExceptionDesc desc) {
        if (!expression) {
            throw new BadRequestException(desc);
        }
    }

    public static void isTrue(boolean expression, ICustomExceptionDesc desc) {
        state(expression, desc);
    }


    public static void isNull(@Nullable Object object, ICustomExceptionDesc desc) {
        if (object != null) {
            throw new BadRequestException(desc);
        }
    }

    public static void notNull(@Nullable Object object, ICustomExceptionDesc desc) {
        if (object == null) {
            throw new BadRequestException(desc);
        }
    }

    public static void hasLength(@Nullable String text, ICustomExceptionDesc desc) {
        if (!StringUtils.hasLength(text)) {
            throw new BadRequestException(desc);
        }
    }

    public static void hasText(@Nullable String text, ICustomExceptionDesc desc) {
        if (!StringUtils.hasText(text)) {
            throw new BadRequestException(desc);
        }
    }

    public static void doesNotContain(@Nullable String textToSearch, String substring, ICustomExceptionDesc desc) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) &&
                textToSearch.contains(substring)) {
            throw new BadRequestException(desc);
        }
    }

    public static void notEmpty(@Nullable Object[] array, ICustomExceptionDesc desc) {
        if (ObjectUtils.isEmpty(array)) {
            throw new BadRequestException(desc);
        }
    }

    public static void noNullElements(@Nullable Object[] array, ICustomExceptionDesc desc) {
        if (array != null) {
            for (Object element : array) {
                if (element == null) {
                    throw new BadRequestException(desc);
                }
            }
        }
    }

    public static void contains(Object element, ICustomExceptionDesc desc, Object... array) {
        boolean contains = false;
        for (Object arrayItem : array) {
            if (arrayItem.equals(element)) {
                contains = true;
                break;
            }
        }
        if (!contains) {
            throw new BadRequestException(desc);
        }
    }

    public static void notEmpty(@Nullable Collection<?> collection, ICustomExceptionDesc desc) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BadRequestException(desc);
        }
    }

    public static void noNullElements(@Nullable Collection<?> collection, ICustomExceptionDesc desc) {
        if (collection != null) {
            for (Object element : collection) {
                if (element == null) {
                    throw new BadRequestException(desc);
                }
            }
        }
    }

    public static void notEmpty(@Nullable Map<?, ?> map, ICustomExceptionDesc desc) {
        if (CollectionUtils.isEmpty(map)) {
            throw new BadRequestException(desc);
        }
    }

}
